package activity

import org.springframework.dao.DataIntegrityViolationException

class SerialController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def search() {
        def results
        if (!params.max) params.max = 10
        if (!params.field && !params.txt) {
            params.field = session.field
            params.txt = session.txt
            
        } else {
            session.field = params.field
            session.txt = params.txt
        }
        if(params.field?.equals("名稱")) {
            results = Serial.findAllByNameLike("%${params.txt}%", params)
        }
        if (results?.size() > 0) {
            flash.message = null
        } else {
            flash.message = '查無結果'
        }
        render(view:"list", model:[serialInstanceList: results, serialInstanceTotal: results?.size()])
    }
    
    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [serialInstanceList: Serial.list(params), serialInstanceTotal: Serial.count()]
    }

    def create() {
        [serialInstance: new Serial(params)]
    }

    def save() {
        def serialInstance = new Serial(params)
        if (!serialInstance.save(flush: true)) {
            render(view: "create", model: [serialInstance: serialInstance])
            return
        }
//        flash.message = message(code: 'default.created.message', args: [message(code: 'serial.label', default: 'Serial'), serialInstance.id])
//        redirect(action: "show", id: serialInstance.id)
        //直接連到新增場次
        redirect(controller: "event", action: "createBySerial", params: ["serial.id": serialInstance.id])
    }

    def show(Long id) {
        def serialInstance = Serial.get(id)
        if (!serialInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'serial.label', default: 'Serial'), id])
            redirect(action: "list")
            return
        }

        [serialInstance: serialInstance]
    }

    def edit(Long id) {
        def serialInstance = Serial.get(id)
        if (!serialInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'serial.label', default: 'Serial'), id])
            redirect(action: "list")
            return
        }

        [serialInstance: serialInstance]
    }

    def update(Long id, Long version) {
        def serialInstance = Serial.get(id)
        if (!serialInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'serial.label', default: 'Serial'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (serialInstance.version > version) {
                serialInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'serial.label', default: 'Serial')] as Object[],
                          "Another user has updated this Serial while you were editing")
                render(view: "edit", model: [serialInstance: serialInstance])
                return
            }
        }

        serialInstance.properties = params

        if (!serialInstance.save(flush: true)) {
            render(view: "edit", model: [serialInstance: serialInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'serial.label', default: 'Serial'), serialInstance.id])
        redirect(action: "show", id: serialInstance.id)
    }

    def delete(Long id) {
        def serialInstance = Serial.get(id)
        if (!serialInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'serial.label', default: 'Serial'), id])
            redirect(action: "list")
            return
        }

        try {
            serialInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'serial.label', default: 'Serial'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'serial.label', default: 'Serial'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def addEvent() {
        def serialInstance = new Serial(params)
        println "name" + serialInstance.name
        if (!serialInstance.save(flush: true)) {
            render(view: "create", model: [serialInstance: serialInstance])
        } else {
            redirect(controller: "event", action: "create", params=['serial.id': serialInstance.id])
        }
    }
}
